﻿#include "stdafx.h"
#include "XListViewIntParser.h"

#include "XBSTR.h"
#include "XString.h"
#include "StrUtil.h"
#include "Util.h"
#include "IXColorDraw.h"
#include "IXImageDraw.h"
#include "IXListView.h"
#include "IXEnumConfigNode.h"

#include "ParseHelper.h"


CXListViewIntParser::CXListViewIntParser() : CXIntParserBase(&TIntCreateControl<IXListView>)
{
    ;
}

CXListViewIntParser::~CXListViewIntParser()
{
    ;
}

// 填充属性
BOOL CXListViewIntParser::FillAttr(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    BOOL bResult = m_ParentParser.FillAttr(pNode, pControl);
    if(!bResult)
        return bResult;

    XBSTR bstrTemp;
    XString strTemp;
    LONG lTemp = 0;
    LONG lTemp2 = 0;
    HRESULT hResult;
    DWORD dwTemp;

    IXListView* pListView = Util::QueryIntf<IXListView>(pControl);
    if(pListView == NULL)
        return FALSE;

    // normalHeight
    if(SUCCEEDED(pNode->GetValue(_T("normalHeight"), &lTemp)))
        pListView->SetNormalHeight(lTemp);

    // selectedHeight
    if(SUCCEEDED(pNode->GetValue(_T("selectedHeight"), &lTemp)))
        pListView->SetSelectedHeight(lTemp);

    // normalBkg
    hResult = pNode->GetValue(_T("normalBkg"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        IXDraw* pBkg = ParseHelper::ParseBackground(bstrTemp);
        if(pBkg != NULL)
        {
            pListView->SetNormalBackgnd(pBkg);
            pBkg->Release();
        }
    }

    // hoverBkg
    hResult = pNode->GetValue(_T("hoverBkg"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        IXDraw* pBkg = ParseHelper::ParseBackground(bstrTemp);
        if(pBkg != NULL)
        {
            pListView->SetHoverBackgnd(pBkg);
            pBkg->Release();
        }
    }

    // selectedBkg
    hResult = pNode->GetValue(_T("selectedBkg"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        IXDraw* pBkg = ParseHelper::ParseBackground(bstrTemp);
        if(pBkg != NULL)
        {
            pListView->SetSelectedBackgnd(pBkg);
            pBkg->Release();
        }
    }

    // proplist
    XPtr<IXEnum> pEnumTemp;
    hResult = pNode->EnumNode(_CONFIG_PROPLIST_NODE_NAME_, _T("Column"), &pEnumTemp);
    CHECK_HRESULT(hResult);

    XPtr<IXEnumConfigNode> pEnum = NULL;
    hResult = pEnumTemp->QueryInterface(XINTFID(IXEnumConfigNode), (void**)&pEnum);
    CHECK_HRESULT(hResult);

    IXConfigNode* pPropNode = NULL;
    while(S_OK == (pEnum->Next(&pPropNode)))
    {
        if(FAILED(pPropNode->GetValue(_T("index"), &lTemp)))
            lTemp = 0;

        if(FAILED(pPropNode->GetValue(_T("width"), &lTemp2)))
            lTemp2 = -1;

        if(FAILED(pPropNode->GetValue(_T("text"), &bstrTemp)))
            bstrTemp = _T("");

        if(lTemp2 == -1)
            lTemp = pListView->InsertColumn(lTemp, bstrTemp);
        else
            lTemp = pListView->InsertColumn(lTemp, bstrTemp, lTemp2);

        if(SUCCEEDED(pPropNode->GetValue(_T("flag"), &bstrTemp)))
        {
            dwTemp = 0;
            if(_tcsstr(bstrTemp, _T("sortable")) != NULL)
                dwTemp |= LCF_Sortable;
            if(_tcsstr(bstrTemp, _T("fixed")) != NULL)
                dwTemp |= LCF_Sortable;
            pListView->SetColumnFlags(lTemp, dwTemp);
        }

        XSAFE_RELEASE(pPropNode);
    }
    XSAFE_RELEASE(pPropNode);

    return bResult;
}
